package com.example.activity

import android.gesture.Gesture
import android.gesture.GestureLibraries
import android.gesture.GestureLibrary
import android.gesture.GestureOverlayView
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.widget.EditText
import java.sql.Time
import java.util.concurrent.TimeUnit

class GestureActivity : AppCompatActivity(),
    GestureOverlayView.OnGesturePerformedListener {

    private lateinit var gestureLibrary: GestureLibrary
    private lateinit var editText: EditText


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gesture)
        gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures)
        editText = findViewById(R.id.input)
        if (!gestureLibrary.load()) {
            finish()
        }
        //设置手势组件属性和监听事件
        val gestureOverlayView = findViewById<GestureOverlayView>(R.id.gesture_view)
        gestureOverlayView.gestureColor = Color.BLACK
        gestureOverlayView.fadeOffset = TimeUnit.SECONDS.toMillis(1)
        gestureOverlayView.addOnGesturePerformedListener(this)
    }

    override fun onGesturePerformed(overlay: GestureOverlayView?, gesture: Gesture?) {
        val gestures = gestureLibrary.recognize(gesture)
        var score: Double = 0.0
        var index: Int = 0
        //循环获取最佳匹配结果
        for (i in 0 until gestures.size) {
            val result = gestures[i]
            if (result.score > score) {
                index = i
                score = result.score
            }
        }
        var text = editText.text.toString()
        text += gestures[index].name
        editText.setText(text)
    }


}